package org.xxpay.pay.service.impl;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.xxpay.core.common.constant.Constant;
import org.xxpay.core.common.constant.MchConstant;
import org.xxpay.core.common.constant.PayEnum;
import org.xxpay.core.common.constant.RetEnum;
import org.xxpay.core.common.domain.api.AbstractRes;
import org.xxpay.core.common.exception.PayException;
import org.xxpay.core.common.exception.ServiceException;
import org.xxpay.core.common.util.MyLog;
import org.xxpay.core.entity.PayOrder;
import org.xxpay.core.entity.PayPassage;
import org.xxpay.core.entity.PayPassageAccount;
import org.xxpay.core.service.IXxPayPassageTestService;
import org.xxpay.pay.channel.BasePayment;
import org.xxpay.pay.service.CommonService;
import org.xxpay.pay.service.LocalDynamicChannelService;

import java.util.Date;

@Service(interfaceName = "org.xxpay.core.service.IXxPayPassageTestService", version = Constant.XXPAY_SERVICE_VERSION, retries = Constant.DUBBO_RETRIES)
public class XxPayPassageTestServiceImpl implements IXxPayPassageTestService {

    private static final MyLog logger = MyLog.getLog(XxPayPassageTestServiceImpl.class);

    @Autowired
    private CommonService commonService;

    @Autowired
    private LocalDynamicChannelService localDynamicChannelService;


    @Override
    public String test(Integer passageAccountId) {

        boolean isPass = false; //是否测试通过
        PayOrder payOrder = new PayOrder();

        try {

            //查询子账号
            PayPassageAccount account = commonService.payPassageAccountService.findById(passageAccountId);
            if(account == null) throw new ServiceException(RetEnum.RET_COMM_PARAM_NOT_FOUND);

            PayPassage passage = commonService.payPassageService.findById(account.getPayPassageId());

            //封装payOrder
            payOrder.setPayOrderId("TEST" + new Date().getTime());
            payOrder.setChannelId(account.getIfCode());
            payOrder.setPassageAccountId(passageAccountId);
            payOrder.setSubject("测试支付Subject");
            payOrder.setBody("测试支付Body");
            payOrder.setAmount(1L); //金额 1分钱

            //调起接口
            BasePayment.IS_TEST_FLAG.set("1");  //设置当前线程变量值表示正在测试, 无需判断账号条件；
            String channelId = payOrder.getChannelId();
            String channelName = channelId.substring(0, channelId.indexOf("_"));

            // 执行支付
            AbstractRes retObj = localDynamicChannelService.callPaymentMethod(channelName, payOrder, false);

            if(PayEnum.OK.getCode().equals(retObj.getRetCode())){
                isPass = true;
            }
        }catch (PayException e) {
            logger.error("异常", e.getMessage());
        }catch (Exception e) {
            logger.error("异常", e);
        }

        PayPassageAccount updateRecord = new PayPassageAccount();
        updateRecord.setId(passageAccountId);
        updateRecord.setTestStatus(isPass ? MchConstant.TEST_STATUS_SUCCESS: MchConstant.TEST_STATUS_FAIL);
        commonService.payPassageAccountService.update(updateRecord); //更新测试状态
        return "测试结果：" + (isPass ? "通过" : "失败") + ", 上送商户订单号为：" + payOrder.getPayOrderId();
    }
}
